package com.busdepot.webapp.domain;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.stereotype.Component;

import com.busdepot.webapp.util.DbUtils;

@Component("partRepository")
public class PartRepositoryImpl implements PartRepository {
	private SimpleJdbcTemplate simpleJdbcTemplate = DbUtils
			.getSimpleJdbcTemplate();

	public List<AbstractPart> getPartById(int id) {
		String sql = "select * from partnumber where id=?";
		return this.simpleJdbcTemplate.query(sql, new PartMapper(), id);
	}

	public List<AbstractPart> getPartByNumber(String partNumber) {
		String sql = "select * from partnumber where partNumber=?";
		return this.simpleJdbcTemplate.query(sql, new PartMapper(), partNumber);
	}

	public void save(AbstractPart abstractPart) {
		/*
		 * if (abstractPart.getId() == 0) { insert(abstractPart); } else {
		 * update(abstractPart); }
		 */
	}

	private void update(AbstractPart abstractPart) {

		// TODO FIXME

	}

	private void insert(AbstractPart abstractPart) {
		
		// TODO FIXME

	}

	private static final class PartMapper implements RowMapper {

		public AbstractPart mapRow(ResultSet rs, int rowNum)
				throws SQLException {
			int id = rs.getInt("id");
			String partNumber = rs.getString("partnumber");
			String name = rs.getString("name");
			String description = rs.getString("description");
			double price = rs.getDouble("price");
			double core = rs.getDouble("core");
			double cost = rs.getDouble("cost");
			boolean hotItem = rs.getBoolean("hotitem");
			boolean special = rs.getBoolean("special");
			String pictureType = rs.getString("picture");
			int vendorId = rs.getInt("vendor_id");
			String availability = rs.getString("availability");
			String DbBusDepot = rs.getString("db_busdepot");
			String DbImc = rs.getString("db_imc");
			double listPrice = rs.getDouble("list");
			String xref = rs.getString("xref");
			double weight = rs.getDouble("weight");
			String unitMeasure = rs.getString("unit_measure");
			String brand = rs.getString("brand");
			String note = rs.getString("note");
			String footnote = rs.getString("footnote");
			String moreInfo = rs.getString("moreinfo");
			/*
			 * AbstractPart abstractPart = new AbstractPart(id, partNumber,
			 * name, description, price, core, cost, hotItem, special,
			 * pictureType, vendorId, availability, DbBusDepot, DbImc,
			 * listPrice, xref, weight, unitMeasure, brand, note, footnote,
			 * moreInfo);
			 * 
			 * return abstractPart;
			 */
			return null;
		}

	}

}
